﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Threading;
using Urd.Packet;
using UnityEngine;

namespace Wernandy.Posclient
{
    class pos:XYZ
    {
        public pos(Vector3 position,int id) :base(id,position.x,position.y,position.z)
        {
            
        }
    }
    class posr : XYZR 
    {
        public posr(Vector3 position,float Yrot ,int id) :base(id,position.x,position.y,position.z,Yrot)
        {
            
        }
    }

    class pospack 
    {
        
        public const byte opcode = (byte)Urd.Packet.Opcode.Pos;
        public const byte command = (byte)Urd.Packet.Command.GetOK;
        private TimeSpan lag;

        const byte packetsize = 16;
        
        public List<XYZ> data;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="buf">принимает буффер прходящего пакета целиком. делает проверки сам</param>
        public pospack()
        {
            this.lag = TimeSpan.Zero;
            data = new List<XYZ>();
        }

        public pospack(byte[] buf,TimeSpan lag) 
        {
            this.lag = lag;
            data = new List<XYZ>();
            byte[] b = new byte[packetsize];
            for (int i = 0; i < buf.Length-(packetsize-1); i += packetsize) 
            {                
                Array.Copy(buf, i, b, 0, packetsize);
                data.Add(new XYZ(b));
            }
        }
        
        public pospack(packet p,TimeSpan lag)
        {
            byte[] b = new byte[packetsize];
            byte[] buf = p.Bytes;
            
            this.lag = lag;
            data = new List<XYZ>();

            for (int i = 3; i < buf.Length - (packetsize - 1); i += packetsize)
            {
                Array.Copy(buf, i, b, 0, packetsize);
                XYZ pp = new XYZ(b);
                if(!pp.isEmpty)
                data.Add(pp);
            }
        }
        public pos Take(int id) 
        {
            
            if(data.Exists(x=>x.ID==id))
            {
                 return (pos)data.Find(x => x.ID == id);
            }
            return new pos(new Vector3(0,0,0),id);
        }

        public List<int> IDlist 
        {
            get 
            {
                List<int> ids = new List<int>();
                foreach (XYZ p in data) 
                {
                    ids.Add(p.ID);
                }
                return ids;
            }
        }
        public byte[] bytes 
        {
            get 
            {
                byte[] buf = new byte[packetsize * data.Count];
                for(int i = 0 ; i < data.Count;i++) 
                {
                    Array.Copy(data[i].bytes, 0, buf, i * packetsize, packetsize);
                }
                return buf;
            }
        }

    }
    class pospackr
    {

        public const byte opcode = (byte)Urd.Packet.Opcode.Pos;
        public const byte command = (byte)Urd.Packet.Command.GetOK;
        private TimeSpan lag;

        const byte packetsize = 20;

        public List<XYZR> data;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="buf">принимает буффер прходящего пакета целиком. делает проверки сам</param>
        public pospackr()
        {
            this.lag = TimeSpan.Zero;
            data = new List<XYZR>();
        }

        public pospackr(byte[] buf, TimeSpan lag)
        {
            this.lag = lag;
            data = new List<XYZR>();
            byte[] b = new byte[packetsize];
            for (int i = 0; i < buf.Length - (packetsize - 1); i += packetsize)
            {
                Array.Copy(buf, i, b, 0, packetsize);
                data.Add(new XYZR(b));
            }
        }

        public pospackr(packet p, TimeSpan lag)
        {
            byte[] b = new byte[packetsize];
            byte[] buf = p.Bytes;

            this.lag = lag;
            data = new List<XYZR>();

            for (int i = 3; i < buf.Length - (packetsize - 1); i += packetsize)
            {
                Array.Copy(buf, i, b, 0, packetsize);
                XYZR pp = new XYZR(b);
                if (!pp.isEmpty)
                    data.Add(pp);
            }
        }
        public XYZR Take(int id)
        {

            if (data.Exists(x => x.ID == id))
            {
                return data.Find(x => x.ID == id);
            }
            return new XYZR(id,0, 0, 0, 0);
        }

        public List<int> IDlist
        {
            get
            {
                List<int> ids = new List<int>();
                foreach (XYZR p in data)
                {
                    ids.Add(p.ID);
                }
                return ids;
            }
        }
        public byte[] bytes
        {
            get
            {
                byte[] buf = new byte[packetsize * data.Count];
                for (int i = 0; i < data.Count; i++)
                {
                    Array.Copy(data[i].bytes, 0, buf, i * packetsize, packetsize);
                }
                return buf;
            }
        }

    }
}
